package com.ruoyi.client.cloudcc.api.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.client.cloudcc.bean.CloudCCFormData;
import com.ruoyi.client.cloudcc.bean.QueryParam;
import com.ruoyi.client.cloudcc.bean.BaseEntity;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.client.cloudcc.api.FormApiService;
import com.ruoyi.client.cloudcc.api.UserApiService;
import com.ruoyi.client.cloudcc.constants.CloudCCConstants;
import com.ruoyi.client.cloudcc.utils.CloudCCAPIUtils;

import javax.annotation.Resource;

@Service
public class FormApiServiceImpl implements FormApiService {
    @Resource
    UserApiService userApiService;
    @Autowired
    private ISysConfigService configService;

    @Override
    public JSONObject cquery(QueryParam queryParam) throws Exception {
        return getApiUtil("cquery",queryParam.getObjectApiName())
                .appendParam("expressions",queryParam.getExpressions())
                .appendParam("isAddDelete",queryParam.isAddDelete())
                .send();
    }

    @Override
    public JSONObject cqueryWithRoleRight(QueryParam queryParam) throws Exception {
        return getApiUtil("cqueryWithRoleRight",queryParam.getObjectApiName())
                .appendParam("expressions",queryParam.getExpressions())
                .appendParam("isAddDelete",queryParam.isAddDelete())
                .send();
    }

    @Override
    public JSONObject insert(CloudCCFormData data) throws Exception {
        return getApiUtil("insert",data.getObjectApiName())
                .appendParam("data",data.getData().toJSONString())
                .send();
    }

    @Override
    public JSONObject insertWithRoleRight(CloudCCFormData data) throws Exception {
        return getApiUtil("insertWithRoleRight",data.getObjectApiName())
                .appendParam("data",data.getData().toJSONString())
                .send();
    }

    @Override
    public BaseEntity delete(String objectApiName, String... ids) throws Exception {
        CloudCCFormData data = new CloudCCFormData(objectApiName);
        for (String id:ids){
            data.next("id",id);
        }
        return getApiUtil("delete",objectApiName)
                .appendParam("data",data.getData().toJSONString())
                .send().toJavaObject(BaseEntity.class);
    }

    @Override
    public BaseEntity delete(String objectApiName, String id) throws Exception {
        CloudCCFormData data = new CloudCCFormData(objectApiName);
            data.next("id",id);
        return getApiUtil("delete",objectApiName)
                .appendParam("data",data.getData().toJSONString())
                .send().toJavaObject(BaseEntity.class);
    }

    @Override
    public BaseEntity deleteWithRoleRight(String objectApiName, String... ids) throws Exception {
        CloudCCFormData data = new CloudCCFormData(objectApiName);
        for (String id:ids){
            data.next("id",id);
        }
        return getApiUtil("deleteWithRoleRight",objectApiName)
                .appendParam("data",data.getData().toJSONString())
                .send().toJavaObject(BaseEntity.class);
    }

    @Override
    public BaseEntity deleteWithRoleRight(String objectApiName, String id) throws Exception {
        CloudCCFormData data = new CloudCCFormData(objectApiName);
            data.next("id",id);
        return getApiUtil("deleteWithRoleRight",objectApiName)
                .appendParam("data",data.getData().toJSONString())
                .send().toJavaObject(BaseEntity.class);
    }

    /**
     * 获取请求工具
     * @param serviceName
     * @return
     */
    private CloudCCAPIUtils getApiUtil(String serviceName,String objectApiName) throws Exception {
        String binding = userApiService.getBinding();
        return CloudCCAPIUtils.createRequest(configService.selectConfigByKey(CloudCCConstants.CloudCC_API_URL), serviceName)
                .appendParam("binding",binding)
                .appendParam("objectApiName",objectApiName);
    }
}
